

import React from "react";
import {
    HashRouter, BrowserRouter, Routes, Route, useRoutes, useLocation, redirect, Navigate
} from "react-router-dom";
import CircularProgress from '@mui/material/CircularProgress'
import NotAuth from "@@/view/NotAuth"
import { isAdmin } from "@@/utils/index"
import ErrorPage from "@@/view/ErrorPage.jsx";



const Loading = () => {
    return (
        <CircularProgress />
    )
}


const HomePage = React.lazy(() => import('../view/pages/home/Dashboard.jsx'))
const ChatPage = React.lazy(() => import('../view/pages/chat/ChatPage.jsx'))

const Login = React.lazy(() => import('../view/Login.jsx'))
const NotFound = React.lazy(() => import('../view/NotFound.jsx'))
// const ErrorPage = React.lazy(() => import('../view/ErrorPage.jsx'))


const adminRouters = ['/dashboard/users/add', '/dashboard/users/list']

const routers = [
    {
        path: "/*",
        index: true,
        element: <HomePage />,
        errorElement: <ErrorPage />
    },
    {
        path: "/dashboard/*",
        element: <HomePage />,
        meta: {
            title: "dashboard页",
            key: "dashboard",

            // superAdmin: true,
            roles: ['superAdmin', 'admin'],
        }
    }
    // , {
    //     path: "/chatlocal",
    //     element: <ChatPage />,
    //     meta: {
    //         title: "聊天页",
    //         key: "chatlocal"
    //     }
    // },
    , {
        path: "/login",
        element: <Login />,
        meta: {
            title: "登录页",
            key: "login"
        }
    },
    , {
        path: "*",
        element: <NotFound />,
        meta: {
            title: "404",
            key: "404"
        }
    },
]



const RouterRender = () => {
    return useRoutes(routers)
}


const onRouteBefore = ({ pathname, meta }) => {
    // 动态修改页面title
    if (meta.title !== undefined) {
        document.title = meta.title
    }

    // 判断未登录跳转登录页
    if (meta.needLogin) {
        if (!isLogin) {
            return '/login'
        }
    }
}


const AuthRoute = (props) => {

    const { pathname } = useLocation()
    console.log('pathname==', pathname)

    if(!pathname || !pathname.startsWith("/")) {
        return <Navigate to="/" />
    }

    const access_token = localStorage.getItem("access_token")

    // 解决重定向无限递归的问题
    if ("/login" === pathname) {
        if (access_token) {
            return <Navigate to="/dashboard" />
        }

        return props.children
    }

    if (!access_token) {
        return <Navigate to="/login" />
    } else {
        let admin = isAdmin()
        // console.log('admin==', admin,  adminRouters.includes(pathname))

        if (!admin && adminRouters.includes(pathname)) {
            return <NotAuth />
        }

        return (
            props.children
        )
    }

}



const EchoUseRoutes3 = () => {

    return (
        <HashRouter>
            <React.Suspense fallback={<Loading />}>
                <AuthRoute>
                    <Routes>
                        {
                            routers.map((item, index) => {
                                return (
                                    <Route key={index} path={item.path} element={item.element} />
                                )
                            })
                        }
                    </Routes>

                    {/* <RouterRender /> */}
                </AuthRoute>
            </React.Suspense>

        </HashRouter>
    )

}

export default EchoUseRoutes3
